package com.znz.web;

import com.znz.domain.Part;
import com.znz.domain.Project;
import com.znz.domain.Tcustomer;
import com.znz.service.CustomerSerService;
import com.znz.service.HospitalService;
import com.znz.service.OrderService;
import com.znz.util.common.Page;
import com.znz.util.common.ReturnObj;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

@RequestMapping("/user")
@Controller
public class UserController extends AbstractController {

    @Autowired
    CustomerSerService customerSerService;
    @Autowired
    HospitalService hospitalService;
    @Autowired
    OrderService orderService;

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 菜单整形项目界面
     * @return
     */
    @RequestMapping(value={"/","/user"})
    public ModelAndView userList(String userName,Page page) {
        try{
            ModelAndView modelAndView=null;
            Map<String,Object> map=new HashMap<String, Object>();
            Page<Tcustomer> tcustomerPage=customerSerService.getCustomerServiceList(userName, page);
            map.put("tcustomerPage",tcustomerPage);
            modelAndView = getTemplate("user/list",map);
            return modelAndView;
        }catch (Exception e){
            logger.error("页面初始化异常", e);
            ModelAndView modelAndView = getTemplate("uncaughtException", null);
            return modelAndView;
        }
    }

    /**
     * 本方法是新增一个客服账户
     * @return
     */
    @RequestMapping(value="toAdd")
    private ModelAndView toAdd(String user) {
        try {
            Map<String,Object> map=new HashMap<String,Object>();
            List<Part> partList=orderService.getPartAll();
            map.put("partList",partList);
            if(null!=partList && partList.size()>0){
                List<Project> projectList=hospitalService.getProjectListByPart(partList.get(0).getId());
                map.put("projectList",projectList);
            }
            //TODO 需要获取项目列表放到map里，用于配置项目列表
            ModelAndView modelAndView = getTemplate("user/add",map);
            return modelAndView;
        } catch (Exception e) {
            logger.error("新增客服页面初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }

    }

    @RequestMapping(value="toEdit")
    private ModelAndView toEdit(Long project_id) {
        try {

            //TODO 这里需要根据客服id获取客服对象放到map里

            Map<String,Object> map=new HashMap<String,Object>();

            //TODO 同时还需要获取项目列表放到map里，用于配置项目列表
            ModelAndView modelAndView = getTemplate("system/project/save",map);

            return modelAndView;
        }catch (Exception e){
            logger.error("修改客服页面初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }
    }

    public ReturnObj saveCustomer(String customer){
        ReturnObj returnObj=null;
        try{
            JSONObject jsonCustomer=JSONObject.fromObject(customer);
            boolean flag=customerSerService.saveCustomerService(jsonCustomer);
            returnObj=ReturnObj.createSuccess(flag);
        }catch (Exception e){
            logger.error(customer);
            return ReturnObj.createError();
        }
        return returnObj;
    }
}
